<?php
if(!defined('IN_IBEIKE')) {
	exit('Access Denied');
}

class table_city_square extends common_table
{
	public function __construct() {

		$this->_table = 'city_square';
		$this->_pk    = 'sid';
		parent::__construct();
	}
	public function insert($data,$return_insert_id=false){
		if($data && is_array($data)){
			return DB::insert($this->_table, $data, $return_insert_id);
		}
		return 0;
	}
	public function fetch_by_tid($tid){
		if(!empty($tid)&&$tid){
			$tid=intval($tid);
			return DB::fetch_first('SELECT * FROM %t WHERE tid=%d',array($this->_table,$tid));//返回数组包含各个字段的值
		}
		return false;
	}
	public function fetch_by_sid($sid){
		if(!empty($sid)&&$sid){
			$sid=intval($sid);
			return DB::fetch_first('SELECT * FROM %t WHERE sid=%d',array($this->_table,$sid));//返回数组包含各个字段的值
		}
		return false;
	}
	public function fetch_all_by_tid_isdel($tid,$isdel=0,$start=0,$limit=0,$gule='>'){
		$tid=intval($tid);
		$isdel=intval($isdel);
		$start=intval($start);
		$limit=intval($limit);
		$gule=helper_util::check_glue($gule);
		$wheresql="";
		if($tid){
			$wheresql.='tid'.$gule.$tid;
		}
		if($wheresql){
			$wheresql.=' AND isdel='.$isdel;
		}else{
			$wheresql.='isdel='.$isdel;
		}
		return DB::fetch_all("SELECT * FROM %t WHERE $wheresql ORDER BY dateline DESC ".DB::limit($start, $limit),array($this->_table));
	}
	public function fetch_all_by_authorid_isdel($authorid,$isdel=0,$start=0,$limit=0){
		$authorid=intval($authorid);
		$isdel=intval($isdel);
		$start=intval($start);
		$limit=intval($limit);
		if($authorid){
			$wheresql='authorid='.$authorid.' AND isdel='.$isdel;
			return DB::fetch_all("SELECT * FROM %t WHERE $wheresql ORDER BY dateline DESC ".DB::limit($start, $limit),array($this->_table,$isdel));
		}else{
			return false;
		}
		
	}
	public function fetch_all_by_tid($tid){
		if(!empty($tid)&&$tid){
			return DB::fetch_all('SELECT * FROM %t WHERE tid=%s',array($this->_table,$tid));//返回数组包含各个字段的值
		}
		return false;
	}
	public function delete_all_by_tid($tid,$limit="",$unbuffered = true){//按主键删除
		if(!empty($tid)&&$tid){
			$condition=$this->_pk."='".$tid."'";
			return DB::delete($this->_table,$condition,limit,$unbuffered = true);//返回受影响的行数
		}
		return false;
	}
	public function update_by_tid($tid,$data,$unbuffered = false, $low_priority = false){
		if(!empty($tid) && !empty($data)){
			$condition=$this->_pk."='".$tid."'";
			return DB::update($this->_table,$data,$condition,$unbuffered,$low_priority);//返回前一次 MySQL 操作所影响的记录行数
		}
		return false;
	}
	public function update_reply_by_tid($tid){
		if(!empty($tid)){
			return DB::query('UPDATE %t SET `replies`=`replies`+1 WHERE tid=%d', array($this->_table, $tid));
		}
	}
	public function update_nice_by_tid($tid){
		if(!empty($tid)){
			return DB::query('UPDATE %t SET `nice`=`nice`+1 WHERE tid=%d', array($this->_table, $tid));
		}
	}
//	public function update_intfield_chage_by_tid($tid,$fielddata,$order){//$fielddata是键为字段,值为修改的数目,$order为1增加0减
/*		if(!empty($tid) && !empty($fielddata) && !empty($order)){
            $sign="";
			foreach ($fielddata as $k => $v){
				$datakey=$sign.$k;
                $sign=",";
			}
			//$old_num=self::get_intfield_num_by_tid($tid,$datakey);//查询该主键的数字字段值,返回数组
			if($old_num!=false){
				foreach ($fielddata as $k => $v){
					if($order==1){
						$old_num[$k]=$old_num[$k]+$v;
					}else{
						$old_num[$k]=$old_num[$k]-$v;
					}
				}
			}else{
				return false;
			}
			$condition=$this->_pk."='".$tid."'";
			return DB::update($this->_table,$old_num,$condition);//更新数据
		}else{
			return false;
		}
	}
	*/
	public static function get_intfield_num_by_tid($tid,$datakey){//按主键查询数字类的字段的值为多少
		if(!empty($tid) && !empty($datakey)){
            return DB::fetch_first('SELECT %s FROM %t WHERE tid=%d',array($datakey,$this->_table,$tid));
		}else{
			return false;
		}
	}
	public function get_new_massage($sid){//获取没显示在前台的最新消息
		if(!empty($sid)){
			$newsid=DB::fetch_first("SELECT * FROM %t lIMIT %d",array($this->_table,1));//获取最上面的那条记录的数组
			if($newsid['sid']>$sid){
				$limit=$newsid['sid']-$sid;
				return DB::fetch_all("SELECT * FROM %t lIMIT %d",array($this->_table,$limit));
			}else{
				return 0;
			}
		}else{
			return false;
		}
	}
	public function get_most_new_massage($limit=3){
		return DB::fetch_all("SELECT * FROM %t lIMIT %d",array($this->_table,$limit));
	}
	public function get_most_hot_massage($limit=3){
		return DB::fetch_all("SELECT * FROM %t ORDER BY replies desc LIMIT %d",array($this->_table,$limit));
	}
	public function fetch_all_by_field($data){//这里写一个公共的方法,$data为字段=>值,用来查询符合这些字段和值的数据
		if(!empty($data) && is_array($data)){
                    $sign="";
                    foreach ($data as $k => $v){
                        $condition=$sign." ".$k."=".$v." ";
                        $sign="AND";
                    }
			return DB::fetch_all("SELECT * FROM %t WHERE %s",array($this->_table,$condition));
		}else{
			return false;
		}
	}

	
}

?>
